﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CNRegex.Web.Base;
using CNRegex.Biz.BizAccess;
using CNRegex.Entity;
using CNRegex.Web.Models;
using Project.Common;
using CNRegex.Enum;
using CNRegex.Entity.Others;
using CNRegex.Biz;

namespace CNRegex.Web.Controllers
{
	public class BlogController : BaseController
	{
		private BizRegexUserBlogCatalog bizRegexUserBlogCatalog;
		private BizRegexUserBlogComment bizRegexUserBlogComment;
		private BizRegexUserBlogRecord bizRegexUserBlogRecord;
		// GET: /Blog/

		public BlogController()
		{
			
		}

		public ActionResult Index(int? p,int? t,Guid? c,int? days)
		{
			ModelBlogIndex model = new ModelBlogIndex() { PageIndex = p, DaysBefore = days ?? 3, ListType = t == null ? EnumListType.最新 : (EnumListType)t, CatalogID = c };
			return View(model);
		}

		public ActionResult Detail(Guid id, int? pageIndex)
		{
			ModelBlogDetail model = new ModelBlogDetail(id) { PageIndex = pageIndex };
			return View(model);
		}

		[HttpPost]
		[ValidateInput(false)]
		public ActionResult Comment(RegexUserBlogComment model,int? pageIndex)
		{
			model.UserID = FrontContext.CurrentRegexUser.ID;
			model.CommentDate = DateTime.Now;
			bizRegexUserBlogComment.Save(model);
			return RedirectToAction("Detail", new { id=model.UserBlogID,pageIndex = pageIndex });
		}

		[HttpPost]
		public ActionResult Recommend(Guid id)
		{
			Guid? nonGuid=null;
			RegexUserBlogRecord model = new RegexUserBlogRecord() { BlogID = id, RecordTime = DateTime.Today, RecordType = (int)EnumRecordType.Recommend, UserID = FrontContext.CurrentRegexUser == null ? nonGuid : FrontContext.CurrentRegexUser.ID };
			bizRegexUserBlogRecord.Save(model);
			return Content("");
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="c">catalogID</param>
		/// <param name="p">pageIndex</param>
		/// <param name="k">keywords</param>
		/// <param name="ok">order key</param>
		/// <param name="d">order direction</param>
		/// <param name="searchBlogEntity"></param>
		/// <returns></returns>
		public ActionResult Search(Guid? c,int? p,string k,string ok,int? d, SearchBlogEntity searchBlogEntity)
		{
			searchBlogEntity.UserBlogCatalogID = c;
			searchBlogEntity.Keywords = k;
			if (string.IsNullOrEmpty(ok) || d == null)
			{
				searchBlogEntity.OrderRecommend = null;
				searchBlogEntity.OrderClick = null;
			}
			else
			{
				if (ok.Trim() == "click" && d == 0)
					searchBlogEntity.OrderClick = EnumOrder.ASC;
				if (ok.Trim() == "click" && d == 1)
					searchBlogEntity.OrderClick = EnumOrder.DESC;
				if (ok.Trim() == "recommend" && d == 0)
					searchBlogEntity.OrderRecommend = EnumOrder.ASC;
				if (ok.Trim() == "recommend" && d == 1)
					searchBlogEntity.OrderRecommend = EnumOrder.DESC;
				if (ok.Trim() == "date" && d == 0)
					searchBlogEntity.OrderCreateDate = EnumOrder.ASC;
				if (ok.Trim() == "date" && d == 1)
					searchBlogEntity.OrderCreateDate = EnumOrder.DESC;
			}

			ModelBlogSearch model = new ModelBlogSearch() { SearchBlog = searchBlogEntity, PageIndex = p };
			return View(model);
		}

		public ActionResult Create()
		{
			ModelBlogCreate model = new ModelBlogCreate();
			return View(model);
		}

		[HttpPost]
		[ValidateInput(false)]
		public ActionResult Create(RegexUserBlog blogEntity,RegexUserBlogContent blogContentEntity)
		{
			ModelBlogCreate model = new ModelBlogCreate(){BlogContentEntity = blogContentEntity,BlogEntity = blogEntity};
			model.Create();
			return View(model);
		}

		public ActionResult Edit(Guid id)
		{
			ModelBlogCreate model = new ModelBlogCreate(id) { };
			return View("Create",model);
		}

		[HttpPost]
		[ValidateInput(false)]
		public ActionResult Edit(RegexUserBlog blogEntity, RegexUserBlogContent blogContentEntity,string c,int? p)
		{
			ModelBlogCreate model = new ModelBlogCreate() { BlogContentEntity = blogContentEntity, BlogEntity = blogEntity };
			model.Save();
			return Redirect(string.Format("/{0}?{1}p={2}&t={3}", FrontContext.CurrentRegexUser.Name, string.IsNullOrEmpty(c) ? "" : string.Format("c={0}&", c), p, (int)EnumExpressionType.myArticle));
		}


	}
}
